package com.sj.command;

import com.sj.common.lang.Const;
import com.sj.pojo.MsgTypeEnum;
import com.sj.pojo.Protocol;
import com.sj.utils.TransferUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2022-07-12
 * Time: 上午 10:55
 * Description: Set命令
 * 用来编写set命令的处理逻辑
 *
 * @author xiaoqi
 * @since 2022-07-12
 */
public class CommandSet extends Command {

    private CommandSet() {

    }

    public static Protocol doing(String msg) {
        return doSet(msg);
    }

    private static Protocol doSet(String msg) {
        String[] instruct = msg.split(" ");
        if (instruct.length < 1) {
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, "错误的命令!");
        } else if (Const.S_ADD.equalsIgnoreCase(instruct[0])) {
            if (instruct.length < 3) {
                return protocol.getProtocol(MsgTypeEnum.RESPONSE, "错误的命令!");
            }
            Set<String> orDefault = set.getOrDefault(instruct[1], new HashSet<String>());
            orDefault.add(TransferUtils.joint(instruct, 2));
            set.put(instruct[1], orDefault);
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, "OK, current size: " + orDefault.size());
        } else if (Const.S_CARD.equalsIgnoreCase(instruct[0])) {
            Protocol check = TransferUtils.check(instruct, 2, true, set);
            if (check != null) {
                return check;
            }
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, "current size is " + set.get(instruct[1]).size());
        } else if (Const.S_IS_MEMBER.equalsIgnoreCase(instruct[0])) {
            Protocol check = TransferUtils.check(instruct, 3, true, set);
            if (check != null) {
                return check;
            }
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, set.get(instruct[1]).contains(instruct[2]) + "");
        } else if (Const.S_MEMBERS.equalsIgnoreCase(instruct[0])) {
            Protocol check = TransferUtils.check(instruct, 2, true, set);
            if (check != null) {
                return check;
            }
            StringBuilder sb = new StringBuilder();
            for (String item : set.get(instruct[1])) {
                sb.append(item);
                sb.append(", ");
            }
            String res = sb.substring(0, sb.length() - 2);
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, res);
        } else if (Const.S_REM.equalsIgnoreCase(instruct[0])) {
            Protocol check = TransferUtils.check(instruct, 3, true, set);
            if (check != null) {
                return check;
            }
            Set<String> setChild = set.get(instruct[1]);
            boolean result = setChild.remove(instruct[2]);
            if (result) {
                set.put(instruct[1], setChild);
            }
            return protocol.getProtocol(MsgTypeEnum.RESPONSE, result + " , current size: " + setChild.size());
        }
        return protocol.getProtocol(MsgTypeEnum.RESPONSE, "do nothing!");
    }
}